/**********************************************************************
 Copyright (C) 2010 Teppo Eeva

 This file is part of Arithmetic Game.
 For more information, see <http://code.google.com/p/arithmetic-game/>

 Arithmetic Game is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Arithmetic Game is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Arithmetic Game.  If not, see <http://www.gnu.org/licenses/>.
 **********************************************************************/

#ifndef ARITHENGINE_H
#define ARITHENGINE_H

#include <QObject>
#include <QTime>
#include "arithquestion.h"
#include "arithgame.h"

class QTimer;

class ArithEngine : public QObject
{
Q_OBJECT
public:
    explicit ArithEngine(QObject *parent = 0);
    ~ArithEngine();

public:
    // Maxtime to complete level
    int maxTime();
    int score();
    int level();
    void handleAnswer( int answer );
    ArithQuestion question();

public Q_SLOTS:

    void setDiffLevel(ArithDiffLevel diffLevel);


private Q_SLOTS:
    void handleTimerEvent();
    void startGame();
    void endGame();


Q_SIGNALS:

    // elapsedTimeChanged is emited when the elapsed time is updated by engine
    void elapsedTimeChanged( int mSec );

    // newQuestionAvailable is emited when new question is generated
    void newQuestionAvailable();

    // correctAnswerSubmitted is emited when user gives a correct answer for a question
    void correctAnswerSubmitted();

    // incorrectAnswerSubmitted is emited when user gives a wrong answer for a question
    void incorrectAnswerSubmitted( int correctAnswer);

    // gameOver is emited when time runs out on Playing state. Total score and reached level will be passed as argumet.
    void gameOver(int score, int level);

    // scoreChanged is emited when score is changed
    void scoreChanged(int score);

    // levelCompleted is emited when user answer correctly 10 questions. Level which was complete is passed as argument
    void levelCompleted( int level );

    // readyToStartNextLevel is emited when next level can be started
    void readyToStartNextLevel( int level );

private:
    enum State
    {
        Idle,
        Playing,
        Bonus
    } m_state;

    int m_score;
    int m_correctCounter; // Correct answer on current level
    int m_incorrectCounter; // Correct answer on current level
    int m_level; // Active level
    int m_bonusFactor;
    int m_levelFactor;
    int m_timerInterval;
    ArithQuestion m_question;
    ArithDiffLevel m_diffLevel; // Difficulty level
    QTime startLevelTime;
    QTime endLevelTime;
    QTime nowTime;
    QTimer* m_timer;
};

#endif // ARITHENGINE_H
